package com.kadron.basket;

import java.io.Serializable;
import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kadron.entity.Discount;
import com.kadron.entity.Item;
import com.kadron.entity.ItemColor;
import com.kadron.entity.ItemMaterial;
import com.kadron.entity.ItemSize;
import com.kadron.entity.KItemImage;
import com.kadron.order.OrderItemPrice;
import com.kadron.session.BasketItem;

public class BasketItemData implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7637717872287122408L;
	
	private static final Log log = LogFactory.getLog(BasketItemData.class);

	private BasketItem basketItem;

	private Item item;

	private ItemColor itemColor;
	private ItemSize itemSize;
	private ItemMaterial itemMaterial;

	private int quantity;
	private OrderItemPrice price;
	private KItemImage img;
	
	private Discount discount;
	private boolean delPrice;
	private boolean delDiscountPrice;

	public BasketItemData() {
		super();
	}

	public BasketItemData(BasketItem basketItem, Item item, ItemColor itemColor,
			ItemSize itemSize, ItemMaterial itemMaterial, int quantity, OrderItemPrice price, KItemImage img) {
		super();
		this.basketItem = basketItem;
		this.item = item;
		this.itemColor = itemColor;
		this.itemSize = itemSize;
		this.itemMaterial = itemMaterial;
		this.quantity = quantity;
		this.price = price;
		this.img = img;
	}

	public BasketItem getBasketItem() {
		return basketItem;
	}

	public void setBasketItem(BasketItem basketItem) {
		this.basketItem = basketItem;
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public ItemColor getItemColor() {
		return itemColor;
	}

	public void setItemColor(ItemColor itemColor) {
		this.itemColor = itemColor;
	}

	public ItemSize getItemSize() {
		return itemSize;
	}

	public void setItemSize(ItemSize itemSize) {
		this.itemSize = itemSize;
	}

	public ItemMaterial getItemMaterial() {
		return itemMaterial;
	}

	public void setItemMaterial(ItemMaterial itemMaterial) {
		this.itemMaterial = itemMaterial;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public OrderItemPrice getPrice() {
		return price;
	}

	public void setPrice(OrderItemPrice price) {
		this.price = price;
	}
	
	public KItemImage getImg() {
		return img;
	}

	public void setImg(KItemImage img) {
		this.img = img;
	}

	public Discount getDiscount() {
		return discount;
	}

	public void setDiscount(Discount discount) {
		this.discount = discount;
	}

	public boolean isDelPrice() {
		return delPrice;
	}

	public void setDelPrice(boolean delPrice) {
		this.delPrice = delPrice;
	}

	public boolean isDelDiscountPrice() {
		return delDiscountPrice;
	}

	public void setDelDiscountPrice(boolean delDiscountPrice) {
		this.delDiscountPrice = delDiscountPrice;
	}
	
	public BigDecimal getSum() {
		BigDecimal quant = new BigDecimal(quantity);
		BigDecimal sum = null;
		if (price != null && price.getYourPrice() != null) {
			sum = price.getYourPrice().multiply(quant);
		} else {
			if (log.isDebugEnabled()) {
				log.debug("price is null");
			}
			sum = new BigDecimal(0);
		}
		return sum;
	}
	
	public boolean isDiscounted() {
		return discount != null && discount.isActive();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((item == null) ? 0 : item.hashCode());
		result = prime * result
				+ ((itemColor == null) ? 0 : itemColor.hashCode());
		result = prime * result
				+ ((itemMaterial == null) ? 0 : itemMaterial.hashCode());
		result = prime * result
				+ ((itemSize == null) ? 0 : itemSize.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BasketItemData other = (BasketItemData) obj;
		if (item == null) {
			if (other.item != null)
				return false;
		} else if (!item.equals(other.item))
			return false;
		if (itemColor == null) {
			if (other.itemColor != null)
				return false;
		} else if (!itemColor.equals(other.itemColor))
			return false;
		if (itemMaterial == null) {
			if (other.itemMaterial != null)
				return false;
		} else if (!itemMaterial.equals(other.itemMaterial))
			return false;
		if (itemSize == null) {
			if (other.itemSize != null)
				return false;
		} else if (!itemSize.equals(other.itemSize))
			return false;
		return true;
	}

}
